package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.FeriaArtesanal;

public class FeriaArtesanalDAOImpl extends JdbcDaoSupport implements IFeriaArtesanalDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByKey findByKeyQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM REG_FERIA_ARTESANAL_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(FeriaArtesanal feriaArtesanal) {
		deleteOperation.execute( feriaArtesanal  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public FeriaArtesanal findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(FeriaArtesanal feriaArtesanal) {
		insertOperation.execute( feriaArtesanal );
	}

	public void update(FeriaArtesanal feriaArtesanal) {
		updateOperation.execute( feriaArtesanal );
	}

	public FeriaArtesanal findByCriteria(FeriaArtesanal  feriaArtesanal) {
		return findByCriteriaQuery.runQuery(feriaArtesanal);
	}

	public List load() {
		return findAllQuery.runQuery();
	}

	public List loadByKey(FeriaArtesanal  feriaArtesanal) {
		return findByKeyQuery.runQuery(feriaArtesanal);
	}
	
	public FeriaArtesanal findByActiveState(FeriaArtesanal  feriaArtesanal) {
		return findByActiveStateQuery.runQuery(feriaArtesanal);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByKeyQuery = new FindByKey(getDataSource());
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO REG_FERIA_ARTESANAL_T ("
				  + " ID, "
				  + " ID_SOLICITUD, "
				  + " ID_TIPO_FERIA, "
				  + " FECHA_INICIO, "
				  + " FECHA_FIN, "
				  + " HORA_INICIO, "
				  + " HORA_FIN, "
				  + " CANTIDAD_TOLDOS, "
				  + " LUGAR, "
				  + " DESCRIPCION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " REG_FERIA_ARTESANAL_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"				  
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));					
			compile();
		}
		public void execute(FeriaArtesanal feriaArtesanal) {
			InsertOperation.this.update( new Object[]{
				feriaArtesanal.getIdSolicitud(),
				feriaArtesanal.getIdTipoFeria(),
				feriaArtesanal.getFechaInicio(),
				feriaArtesanal.getFechaFin(),
				feriaArtesanal.getHoraInicio(),
				feriaArtesanal.getHoraFin(),
				feriaArtesanal.getCantidadToldos(),
				feriaArtesanal.getLugar(),
				feriaArtesanal.getDescripcion(),
				feriaArtesanal.getEstado(),
				feriaArtesanal.getUsuarioCrea()							
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE REG_FERIA_ARTESANAL_T SET "					  
    				  + " ID_SOLICITUD = ? ,"
    				  + " ID_TIPO_FERIA = ? ,"
					  + " FECHA_INICIO = ? ,"
					  + " FECHA_FIN = ? ,"
					  + " HORA_INICIO = ? ,"
					  + " HORA_FIN = ? ,"
					  + " CANTIDAD_TOLDOS = ? ,"
					  + " LUGAR = ? ,"
					  + " DESCRIPCION = ? ,"
					  + " ESTADO = ? ,"					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE 
				  	  + "ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));					
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(FeriaArtesanal feriaArtesanal) {
			UpdateOperation.this.update( new Object[]{
				feriaArtesanal.getIdSolicitud(),
				feriaArtesanal.getIdTipoFeria(),
				feriaArtesanal.getFechaInicio(),
				feriaArtesanal.getFechaFin(),
				feriaArtesanal.getHoraInicio(),
				feriaArtesanal.getHoraFin(),
				feriaArtesanal.getCantidadToldos(),
				feriaArtesanal.getLugar(),
				feriaArtesanal.getDescripcion(),
				feriaArtesanal.getEstado(),				
				feriaArtesanal.getUsuarioModifica(),
				feriaArtesanal.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_ARTESANAL_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(FeriaArtesanal feriaArtesanal) {
			this.update( new Object[]{
				feriaArtesanal.getId()
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_ARTESANAL_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			FeriaArtesanal feriaArtesanal = new FeriaArtesanal();
				feriaArtesanal.setId(new Long(rs.getLong("ID")));
				feriaArtesanal.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
				feriaArtesanal.setIdTipoFeria(new Long(rs.getLong("ID_TIPO_FERIA")));
				feriaArtesanal.setIdTipoFeriaStr(rs.getString("ID_TIPO_FERIA"));
				feriaArtesanal.setFechaInicio(rs.getDate("FECHA_INICIO"));		
				feriaArtesanal.setFechaFin(rs.getDate("FECHA_FIN"));	
				feriaArtesanal.setHoraInicio(rs.getString("HORA_INICIO"));
				feriaArtesanal.setHoraFin(rs.getString("HORA_FIN"));
				feriaArtesanal.setCantidadToldos(new Long(rs.getLong("CANTIDAD_TOLDOS")));
				feriaArtesanal.setCantidadToldosStr(rs.getString("CANTIDAD_TOLDOS"));
				feriaArtesanal.setLugar(rs.getString("LUGAR"));
				feriaArtesanal.setDescripcion(rs.getString("DESCRIPCION"));
				feriaArtesanal.setEstado(rs.getString("ESTADO"));
				feriaArtesanal.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				feriaArtesanal.setFechaCrea(rs.getDate("FECHA_CREA"));		
				feriaArtesanal.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				feriaArtesanal.setFechaModifica(rs.getDate("FECHA_MODIFICA"));				
			return feriaArtesanal;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE +" 1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaArtesanal runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (FeriaArtesanal) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " AND ID_SOLICITUD = ? "
										+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public FeriaArtesanal runQuery(FeriaArtesanal feriaArtesanal) {
			List result =  execute( new Object[]{
				feriaArtesanal.getIdSolicitud()
				});
			if ( result.size() > 0 ) {
        		return (FeriaArtesanal) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindByKey extends BaseQueryOperation {
		private static final String sql = "SELECT FERIA.*, RADICADO FROM REG_FERIA_ARTESANAL_T FERIA INNER JOIN "
										+ " ADM_SOLICITUD_T SOL ON FERIA.ID_SOLICITUD=SOL.ID"
										+ WHERE + " SOL.ID =?";
    	public FindByKey(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(FeriaArtesanal feriaArtesanal) {
    		List result =  execute( new Object[]{
    					feriaArtesanal.getIdSolicitud()
    					});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }    
    
    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE +" 1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaArtesanal runQuery(FeriaArtesanal feriaArtesanal) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (FeriaArtesanal) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
